<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>领料管理</title>
<%@ include file="common.jsp"%>
<script type="text/javascript">
$(function() {
	$(".form_datetime").datetimepicker({
		/* 显示格式 */       
		format: "yyyy-mm-dd",
	    /* 完成关闭 */
	    autoclose: true,
	   /* 今天 时间按钮*/
	    todayBtn: true, 
	   /*  今天的时间变黄色 */
	    todayHighlight:true, 
	    /* 定位 */
	    pickerPosition: "bottom-left",
	    /* 最小值 */
	    minuteStep: 10,
	    /*  语言 */
	    language: 'zh-CN',  
		/* 分上午下午 */
	    showMeridian: true,
	    /* 分的所有 */
	    minuteStep:true,
	    /* 月视图 */
	    startView: 2,
	    /* 日期时间选择器所能够提供的最精确的时间选择视图 */ 
		minView: 2
	}).on('hide',function(e) {  
		e.stopPropagation(); //e.stopPropagation()阻止事件冒泡
	});
});
</script>
<script type="text/javascript">
	$(function() {
		//1.初始化Table
		var oTable = new TableInit();
		oTable.Init();

		//2.初始化Button的点击事件
		var oButtonInit = new ButtonInit();
		oButtonInit.Init();
		//点击查询按钮事件
		$("#btn_query").click(function() {
			if($("#endDate").val() == '' || $("#endDate").val() == null) {
				var now = new Date();
				var year = now.getFullYear(); //年
				var month = now.getMonth() + 1; //月
				if(month < 10){
					month = "0" + month;
				}
				var day = now.getDate(); //日
				if(day < 10){
					day = "0" + day;
				}
				var time = year + "-" + month + "-" + day;
				$("#endDate").val(time);
			}
			$("#tb_departments").bootstrapTable('refresh', {
				url : '${pageContext.request.contextPath }/mater/selectByTime.do'
			});
		});

		//删除按钮事件
		$("#btn_delete").click(
			function() {
				//取表格的选中行数据
				var arrselections = $("#tb_departments").bootstrapTable('getSelections');
				if (arrselections.length <= 0) {
					toastr.warning('请选择有效数据');
					return;
				}
				var ids = "";
				for(var i = 0;i < arrselections.length; i++){
					if(i != arrselections.length - 1){
						ids += arrselections[i].pickOdd + ",";
					}else{
						ids += arrselections[i].pickOdd;
					}
				}
				Ewin.confirm({message : "确认要删除选择的数据吗？"}).on(
					function(e) {
						if (!e) {return;}
						$.ajax({
							type : 'POST',
							url : '${pageContext.request.contextPath }/mater/delete.do',
							data : 'pickOdd=' + ids,
							success : function(result) {
								var result = eval("("+ result+ ")");
								if (result.success > 0) {
									$("#tb_departments").bootstrapTable('refresh',{
										url : '${pageContext.request.contextPath }/mater/select.do'
									});
									$("#detailList").bootstrapTable("refresh",{
										url:"${pageContext.request.contextPath }/mater/selectDet.do"
									});
									toastr.success('删除成功');
								}else if(result.success = -1){
									toastr.error('删除失败，部分信息已提交审核不能删除');
								} else {
									toastr.error('删除失败');
								}
							}
						});
					});
			});
		//审批
		$("#btn_approval").click(
				function() {
					//取表格的选中行数据
					var arrselections = $("#tb_departments").bootstrapTable('getSelections');
					if (arrselections.length <= 0) {
						toastr.warning('请选择有效数据');
						return;
					}
					var ids = "";
					for(var i = 0;i < arrselections.length; i++){
						if(arrselections[i].auditor != null && arrselections[i].auditor != ("")){
							toastr.warning('请选择未审批的数据');
							return;
						}
						if(i != arrselections.length - 1){
							ids += arrselections[i].pickOdd + ",";
						}else{
							ids += arrselections[i].pickOdd;
						}
					}
					$.ajax({
						type : "post",
						url : "${pageContext.request.contextPath }/mater/approval.do",
						data : 'pickOdd=' + ids,
						success : function(data) {
							if (data > 0) { 
								toastr.success('审批成功');
								$("#tb_departments").bootstrapTable('refresh');
							}
						}
					});
						
				});
		//撤销审批
		$("#btn_revoApproval").click(
			function() {
				//取表格的选中行数据
				var arrselections = $("#tb_departments").bootstrapTable('getSelections');
				if (arrselections.length <= 0) {
					toastr.warning('请选择有效数据');
					return;
				}
				var ids = "";
				for(var i = 0;i < arrselections.length; i++){
					if(arrselections[i].auditor == null || arrselections[i].auditor == ("")){
						toastr.warning('请选择已审批的数据');
						return;
					}
					if(i != arrselections.length - 1){
						ids += arrselections[i].pickOdd + ",";
					}else{
						ids += arrselections[i].pickOdd;
					}
				}
				$.ajax({
					type : "post",
					url : "${pageContext.request.contextPath }/mater/calApp.do",
					data : 'pickOdd=' + ids,
					success : function(data) {
						if (data > 0) { 
							toastr.success('撤销审批成功');
							$("#tb_departments").bootstrapTable('refresh');
						}
					}
				});
		});
		
		
	});
	var TableInit = function() {
		var oTableInit = new Object();
		//初始化Table
		oTableInit.Init = function() {
			$('#tb_departments').bootstrapTable({
				url : '${pageContext.request.contextPath }/mater/select.do', //请求后台的URL（*）
				method : 'get', //请求方式（*）
				toolbar : '#toolbar', //工具按钮用哪个容器
				pagination : true, //是否显示分页（*）
				striped : true, //是否显示行间隔色
				sortable : false, //是否启用排序
				sortOrder : "asc", //排序方式
				queryParams : oTableInit.queryParams,//传递参数（*）
				sidePagination : "server", //分页方式：client客户端分页，server服务端分页（*）
				pageNumber : 1, //初始化加载第一页，默认第一页
				pageSize : 10, //每页的记录行数（*）
				pageList : [ 10, 20, 30 ], //可供选择的每页的行数（*）
				strictSearch : true,
				clickToSelect : true, //是否启用点击选中行
				height : 300, //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
				uniqueId : "ID", //每一行的唯一标识，一般为主键列
				detailView : false, //是否显示父子表
				columns : [ {
					checkbox : true
				}, {
					field : 'pickOdd',
					align : 'center',
					title : '领料单号'
				}, {
					field : 'pickDate',
					align : 'center',
					title : '领料时间'
				}, {
					field : 'pickMan',
					align : 'center',
					title : '领料人'
				}, {
					field : 'auditor',
					align : 'center',
					title : '审核人'
				}, {
					field : 'remark1',
					align : 'center',
					title : '审核状态',
					formatter:function(value,row,index){
						if(row.remark1 == "1"){
							return "未提交审批";
						}else if(row.remark1 == "2"){
							return "已审批";
						}else{
							return "审核失败";
						}
					}
				}, {
					field : 'remark2',
					align : 'center',
					title : '领料状态',
					formatter:function(value,row,index){
						if(row.remark2 == "1"){
							return "未领料";
						}else{
							return "已领料";
						}
					}
				} ],
				onClickRow: function (row) {
					$("#detailList").bootstrapTable("refresh",{
						url:"${pageContext.request.contextPath }/mater/selectDet.do?remark02="+row.pickOdd
					});
				}
			});
			//明细表的初始化
			$('#detailList').bootstrapTable({
				method : 'get', //请求方式（*）
				striped : true, //是否显示行间隔色
				cache : false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
				pagination : true, //是否显示分页（*）
				sortable : false, //是否启用排序
				sortOrder : "asc", //排序方式
				queryParams : oTableInit.queryParams,//传递参数（*）
				sidePagination : "server", //分页方式：client客户端分页，server服务端分页（*）
				pageNumber : 1, //初始化加载第一页，默认第一页
				pageSize : 10, //每页的记录行数（*）
				pageList : [ 10, 20, 30 ], //可供选择的每页的行数（*）
				clickToSelect : true, //是否启用点击选中行
				height : 300, //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
				uniqueId : "ID", //每一行的唯一标识，一般为主键列
				detailView : false, //是否显示父子表
				columns : [ {
					field : 'materialName',
					align : 'center',
					title : '物料名称'
				}, {
					field : 'remark01',
					align : 'center',
					title : '型号'
				}, {
					field : 'materialUnit',
					align : 'center',
					title : '计量单位'
				}, {
					field : 'requireCount',
					align : 'center',
					title : '需求数量'
				}, {
					field : 'xianbianCount',
					align : 'center',
					title : '线边库库存'
				}, {
					field : 'lingliaoRequireCount',
					align : 'center',
					title : '应领料量'
				}],
			});
		};
		//得到查询的参数
		oTableInit.queryParams = function(params) {
			var temp = { //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
				limit : params.limit, //页面大小
				offset : params.offset,
				startDate : $("#startDate").val(),
				endDate : $("#endDate").val()
			};
			return temp;
		};
		return oTableInit;
	};
	var ButtonInit = function() {
		var oInit = new Object();
		var postdata = {};

		oInit.Init = function() {
			//初始化页面上面的按钮事件
		};
		return oInit;
	};
	//时间框赋值
	function changeSelect() {
		$("#endDate").val(CurentTime());
		var dm = $("#acquisitionTime").val();
		if (dm == "1") {
			$("#startDate").val(changeTime1());
		} else if (dm == "3") {
			$("#startDate").val(changeTime3());
		} else {
			$("#startDate").val(null);
			$("#endDate").val(null);
		}
	}
	//导出当前页
	function doExport() {
		Ewin.confirm({message : "确认要导出所有的数据吗？"}).on(
			function(e) {
				if (!e) {return;}
				window.location.href = "${pageContext.request.contextPath }/mater/export.do";
			});
	}
	//导出领料单明细
	function doExportDetail() {
		var rowsdata = $("#detailList").bootstrapTable('getData');
		if(rowsdata.length != 0){
			Ewin.confirm({message : "确认要导出所有的数据吗？"}).on(
				function(e) {
					if (!e) {return;}
					window.location.href = "${pageContext.request.contextPath }/mater/exportDetail.do";
				});
		}else{
			toastr.warning('您选择的领料单没有明细');
		}
	}
	//获取当前时间
	function CurentTime() {
		var now = new Date();
		var year = now.getFullYear(); //年
		var month = now.getMonth() + 1; //月
		if(month < 10){
			month = "0" + month;
		}
		var day = now.getDate(); //日
		if(day < 10){
			day = "0" + day;
		}
		var time = year + "-" + month + "-" + day;
		return time;
	}
	//3个月的选择赋值
	function changeTime3() {
		var now = new Date();
		var year = now.getFullYear(); //年
		var month = 0;
		if (now.getMonth() == 2) {
			month = 12;
			year = year - 1;
		} else if (now.getMonth() == 1) {
			month = 11;
			year = year - 1;
		} else if (now.getMonth() == 0) {
			month = 10;
			year = year - 1;
		} else {
			month = now.getMonth() - 2; //月
		}
		var day = now.getDate(); //日
		if(month < 10){
			month = "0" + month;
		}
		if(day < 10){
			day = "0" + day;
		}
		var time = year + "-" + month + "-" + day;
		return time;
	}
	//一个月的赋值
	function changeTime1() {
		var now = new Date();
		var year = now.getFullYear(); //年
		var month = 0;
		if (now.getMonth() == 0) {
			month = 12;
			year = year - 1;
		} else {
			month = now.getMonth(); //月
		}
		var day = now.getDate(); //日
		if(month < 10){
			month = "0" + month;
		}
		if(day < 10){
			day = "0" + day;
		}
		var time = year + "-" + month + "-" + day;
		return time;
	}
	function funAdd() {
		window.location = "${pageContext.request.contextPath }/mater/selectOdd.do";
	}
	toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
</script>
</head>
<body>
	<div class="panel-body">
		<div class="panel panel-default">
			<div class="panel-heading">查询条件</div>
			<div class="panel-body">
				<form id="formSearch" class="form-horizontal">
					<div class="form-group" style="margin-top: 15px">
						<label class="control-label col-sm-1" for="pickDate">领料时间:</label>
						<div class="col-sm-3">
							<select class="selectpicker show-tick form-control"
								id="acquisitionTime" onchange="changeSelect()">
								<option value="0">--请选择--</option>
								<option value="1">最近一个月</option>
								<option value="3">最近三个月</option>
							</select>
						</div>
						<label class="control-label col-sm-1" for="startDate">从</label>
						<div class="col-sm-3" style="width: 200px;">
							<input type="text" name="startDate" id="startDate"
								placeholder="yyyy-mm-dd" class="form_datetime form-control" 
									readonly="readonly">
						</div>
						<label class="control-label col-sm-1" for="endDate"
							style="width: 10px;">至</label>
						<div class="col-sm-3" style="width: 200px;">
							<input type="text" id="endDate" name="endDate" placeholder="yyyy-mm-dd" 
								class="form_datetime form-control" readonly="readonly">
						</div>
						<div class="col-sm-2" align="right">
							<button type="button" style="width: 150px;" id="btn_query"
								class="btn btn-primary">查询</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div id="toolbar" class="btn-group" style="margin-top: -20px;">
			<button id="btn_add" type="button" class="btn btn-default"
				onclick="funAdd()">
				<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
			</button>
			<button id="btn_approval" type="button" class="btn btn-default">
				<span class="glyphicon glyphicon-pawn" aria-hidden="true"></span>审批
			</button>
			<button id="btn_revoApproval" type="button" class="btn btn-default">
				<span class="glyphicon glyphicon-bishop" aria-hidden="true"></span>撤销审批
			</button>
			<button id="btn_delete" type="button" class="btn btn-default">
				<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
			</button>
			<button id="btn_export" type="button" class="btn btn-default" onclick="doExportDetail()">
				<span class="glyphicon glyphicon-export" aria-hidden="true"></span>导出当前领料明细
			</button>
			<button id="btn_hide" type="button" class="btn btn-default" style="display: none;">
				<span class="glyphicon glyphicon-cancel" aria-hidden="true"></span>隐藏
			</button>
		</div>
		<table id="tb_departments"></table>
		<br>
		<table id="detailList"></table>
	</div>
</body>
<style>
.table th {
	text-align: center;
}
</style>
</html>